'''
Created on 2010-3-3

@author: Administrator
'''
from jolly.datastructures.AbstractMethod import AbstractMethod
from jolly.datastructures.Association import Association
from jolly.datastructures.String import String
from jolly.datastructures.SearchableContainer import SearchableContainer

class HashTable(SearchableContainer):
    '''
    Base class from which all hash tables are derived.
    '''


    def __init__(self):
        '''
        (HashTable) -> None
        Constructs this hash table.
        '''
        super(HashTable, self).__init__()
    
    def __len__(self): pass
    __len__ = AbstractMethod(__len__)
    
    def getLoadFactor(self):
        '''
        (HashTable) -> double
        Returns the load factor of this hash table.
        '''
        return self.count/len(self)
    loadFactor=property(fget=lambda self: self.getLoadFactor)
    
    def f(self, obj):
        '''
        (HashTable, Object) -> int
        Returns the hash of the given object.
        '''
        return hash(obj)
    
    def g(self, x):
        '''
        (HashTable, int) -> int
        Hashes an integer using the division method of hashing.
        '''
        return abs(x)%len(self)
    
    def h(self, obj):
        '''
        (HashTable, Object) -> int
        Hashes the specified object
        using the composition of the methods f and g.
        '''
        return self.g(self.f(obj))
    
    @staticmethod
    def test(hashTable):
        "HashTable test program."
        print HashTable.test.__doc__
        print hashTable
        hashTable.insert(Association(String("foo"), 12))
        hashTable.insert(Association(String("bar"), 34))
        hashTable.insert(Association(String("foo"), 56))
        print hashTable
        obj = hashTable.find(Association(String("foo")))
        print obj
        hashTable.withdraw(obj)
        print hashTable
        for a in hashTable:
            print a
        